package main

import (
	"fmt"
	"time"
)

func main() {
	//当前时间
	now := time.Now()
	fmt.Println(now)
	fmt.Println(now.Year())
	fmt.Println(now.Month())

	fmt.Println(now.Date())
	fmt.Println(now.Hour())
	fmt.Println(now.Minute())
	fmt.Println(now.Second())
	// 时间戳Unix
	fmt.Println(now.Unix())
	fmt.Println(now.UnixNano())
	//时间格式再转换
	fmt.Println(time.Unix(1564803667, 0)) // 转换成时间

	// 时间间隔
	fmt.Println(time.Second)
	// now + 24小时
	fmt.Println(now.Add(24 * time.Hour))
	// 定时器
	//timer := time.Tick(time.Second)
	//for t := range timer {
	//	fmt.Println(t) // 一秒钟执行一次
	//}
	// 格式化时间 把语言中时间对象 转换成字符串类型的时间
	// 2019-08-03
	fmt.Println(now.Format("2006-01-02"))
	// 2019/08/03 11:55:02
	fmt.Println(now.Format("2006/01/02 15:04:05"))
	// 2019/08/03 11:55:02 AM
	fmt.Println(now.Format("2006/01/02 03:04:05 PM"))
	// 2019/08/03 11:55:02.342
	fmt.Println(now.Format("2006/01/02 15:04:05.000"))

	// 按照对应时间的格式解析字符串类型的时间
	timeobj, err := time.Parse("2006-01-02", "2000-08-03")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(timeobj)
	fmt.Println(timeobj.Unix())
	// 两个时间相减
	d := now.Sub(timeobj)
	loc, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		fmt.Println("loadloc failed,er:%v\n", err)
	}
	fmt.Println(d)
	fmt.Println(loc)
	timeobjs, err := time.ParseInLocation("2006-01-02 15:04:05", "2000-08-03 14:41:50", loc)
	fmt.Println(timeobjs)
	time.Sleep(time.Second)
	n := 10 // int
	time.Sleep(time.Duration(n) * time.Second)
}
